import Vue from 'vue';
import {
  Notification,
  Loading,
  Message
} from 'element-ui';

//add one
function addOneData(dataName, name, item) {
  let reLoading = Loading.service();
  let arr = JSON.parse(window.localStorage.getItem(dataName));
  let bool = false;
  if (arr.length > 0) {
    arr.filter((ite) => {
      if (ite[name] === item[name] && ite.status === '1') {
        console.log(true)
        bool = true;
      } else {
        arr.push(item);
      }
    });
  } else {
    console.log(1)

  }
  if (bool) {
    Notification.warning('此数据已存在！');
    setTimeout(() => {
      reLoading.close();
    }, 300);
    return;
  } else {
    window.localStorage.setItem(dataName, JSON.stringify(arr));
    setTimeout(() => {
      message('成功');
      reLoading.close();
    }, 300);
  }
  arr = [];
}

//add data ok
function add(dataName, name, item) {
  // console.log(item);
  let time = 0;
  if (time > 10) {
    setTimeout(() => {
      time = 0;
    }, 2000)
    return;
  }
  let reLoading = Loading.service();
  let arr = JSON.parse(window.localStorage.getItem(dataName));
  let bool = false;
  if (arr.length > 0) {
    arr.map((ite) => {
      if (ite.No == item.MNo && ite.children && ite.children.length > 0) {
        ite.children.map((mo) => {
          if (item[name] === mo[name] && mo.status === '1') {
            bool = true;
          } else {
            ite.children.push(item);
          }
        });
      } else {
        if (ite[name] === item[name] && ite.status === '1') {
          bool = true;
        } else {
          arr.push(item);
        }
      }
    });
  } else {
    arr.push(item);
  }
  if (bool) {
    Notification.warning('此数据已存在！');
    setTimeout(() => {
      time = 11;
      reLoading.close();
    }, 300);
    return;
  } else {
    window.localStorage.setItem(dataName, JSON.stringify(arr));
    setTimeout(() => {
      message('成功');
      time = 11;
      reLoading.close();
    }, 300);
  }
  arr = [];
}

//查询 根据不同条件
function getDataBySome(dataName, obj) {
  let reLoading = Loading.service();
  let object = JSON.parse(window.localStorage.getItem(dataName));
  let result = [];
  if (object.length > 0) {
    object.map((item) => {
      if (obj.length < 2) {
        obj.filter((arr) => {
          let key = Object.keys(arr);
          if (item[key] === arr[key]) {
            result.push(item);
          } else {
            result;
          }
        })
      } else {
        let arr1 = obj[0];
        let arr2 = obj[1];
        let key1 = Object.keys(arr1);
        let key2 = Object.keys(arr2);
        if (item[key1] === arr1[key1] && item[key2] === arr2[key2]) {
          result.push(item);
        }
      }
    });
  } else {
    result = [];
  }
  setTimeout(() => {
    reLoading.close();
  }, 100);
  return result;
};

//Edit OK
function editdata(objName, item, No) {
  if (!objName || !item || !No) {
    return;
  }
  let reLoading = Loading.service();
  let object = JSON.parse(window.localStorage.getItem(objName));
  let index = '';
  object.forEach((element, i) => {
    if (element.No === No) {
      index = i;
      element = item;
    }
  });
  object[index] = item;
  window.localStorage.setItem(objName, JSON.stringify(object));
  setTimeout(() => {
    reLoading.close();
  }, 300);
}

//delete data
function deleteItem(objName, Nolist) {
  if (!objName && Nolist.length <= 0) {
    return;
  }
  let reLoading = Loading.service();
  let object = JSON.parse(window.localStorage.getItem(objName));
  let ob;
  if (object.length > 0) {
    ob = object.map((item) => {
      Nolist.map(e => {
        if (item.No === e) {
          item.status = '0';
        }
      })
      return item;
    })
  } else {
    return;
  }
  window.localStorage.setItem(objName, JSON.stringify(ob));
  setTimeout(() => {
    reLoading.close();
  }, 300);
}

// 根据No删除数据，MNo：父级；No：子级
function deleteChildrenItemByNo(objName, MNo, No) {
  if (!objName || !MNo || !No) {
    return;
  }
  let reLoading = Loading.service();
  let object = JSON.parse(window.localStorage.getItem(objName));
  let ob;
  if (object.length > 0) {
    ob = object.map((item) => {
      if (item.No === MNo) {
        item.children.map((chi) => {
          if (chi.No === No) {
            chi.status = '0';
          }
        });
      }
      return item;
    })
  } else {
    return;
  }
  window.localStorage.setItem(objName, JSON.stringify(ob));
  setTimeout(() => {
    reLoading.close();
    message("成功");
  }, 300);
}

//success info
function message(message, type) {
  if (arguments.length === 0) {
    return;
  }
  if (arguments.length === 1) {
    Message({
      showClose: true,
      message: message
    });
    return;
  }
  Message({
    showClose: true,
    message: message,
    type: type
  });
}

//get all data
function getAll(objName) {
  if (objName) {
    let object = JSON.parse(window.localStorage.getItem(objName));
    let ob;
    if (object.length > 0) {
      ob = object.filter((item) => {
        if (item !== null && item != undefined && item.No && item.status !== '0') {
          return item;
        }
      });
    } else {
      ob = [];
    };
    window.localStorage.setItem(objName, JSON.stringify(ob));
    return ob;
  } else {
    return;
  }
}

function MonthsEcharts() {
  let reLoading = Loading.service();
  let object = JSON.parse(window.localStorage.getItem("order-manage"));
  let index = 1;
  let arr = [];
  let month = 0;
  let model = {}; //
  if (object.length > 0) {
    month = (new Date()).getMonth();
    arr = object.filter((item) => {
      if (item.adminStatusNo === '12' || item.adminStatusNo === '13' || item.adminStatusNo === '14') {
        let toMonth = getmonth(item.No); //数据在那个月呀
        let days = getDateList(item.No); //每月多少天
        let da = days[toMonth];
        let todate = today(item.No); //当天
        let dateArr = daylist(da); // 遍历当月每天
        let numL = Array(da); //当月数据长度
        numL.fill(0);
        let n = numL[todate] + 1;
        numL.fill(n, todate - 1, todate);
        if (month == toMonth) {
          if (index == 1) {
            model = {
              moth: (month + 1),
              datelist: dateArr,
              numberList: numL
            }
          } else {
            model.numberList[todate] = model.numberList[todate] + 1;
          }
          index++;
          return;
        }
      }
    })
  }
  setTimeout(() => {
    reLoading.close();
  }, 300);
  return model;
}

function getRework() {
  let reLoading = Loading.service();
  let object = JSON.parse(window.localStorage.getItem("order-manage"));
  let index = 1;
  let arr = [];
  let month = 0;
  let model = {}; //
  if (object.length > 0) {
    month = (new Date()).getMonth();
    arr = object.filter((item) => {
      if (item.adminStatusNo === '14') {
        let toMonth = getmonth(item.No); //数据在那个月呀
        let days = getDateList(item.No); //每月多少天
        let da = days[toMonth];
        let todate = today(item.No); //当天
        let dateArr = daylist(da); // 遍历当月每天
        let numL = Array(da); //当月数据长度
        numL.fill(0);
        let n = numL[todate] + 1;
        numL.fill(n, todate - 1, todate);
        if (month == toMonth) {
          if (index == 1) {
            model = {
              moth: (month + 1),
              datelist: dateArr,
              numberList: numL
            }
          } else {
            model.numberList[todate] = model.numberList[todate] + 1;
          }
          index++;
          return;
        }
      }
    })
  }
  setTimeout(() => {
    reLoading.close();
  }, 300);
  return model;
}

let getDateList = function (day) {
  let today = new Date(parseInt(day));
  let year = today.getFullYear();
  let daylist = [];
  let days;
  if (year % 4 > 0 && year % 400 > 0) {
    days = 28;
  } else {
    days = 29;
  }
  daylist = [31, days, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  return daylist;
}

let today = function (parm) {
  let day = parseInt(parm);
  let today = new Date(day);
  let date = today.getDate();
  return date;
}

let getmonth = function (parm) {
  let date = parseInt(parm);
  let day = new Date(date);
  let month = day.getMonth();
  return month;
}
let daylist = function (date) {
  let arr = [];
  for (let i = 1; i <= date; i++) {
    arr.push(i);
  }
  return arr;
}



module.exports = {
  add: add,
  edit: editdata,
  select: getDataBySome,
  allData: getAll,
  delete: deleteItem,
  deleteByNO: deleteChildrenItemByNo,
  echarts: MonthsEcharts,
  echReork: getRework,
  addOne: addOneData
}
